package com.zhsj.OkeHomeConsumption.ui.activity.mvp;

import android.content.Context;

import com.hjq.base.BaseDialog;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.OkeHomeConsumptionApi;
import com.zhsj.OkeHomeConsumption.network.HttpClient;
import com.zhsj.OkeHomeConsumption.network.JsonResponseHandler;
import com.zhsj.OkeHomeConsumption.other.TimeChecker;
import com.zhsj.OkeHomeConsumption.other.URLEntity;
import com.zhsj.OkeHomeConsumption.ui.activity.entering.mvp.MerchantUIInterFace;
import com.zhsj.OkeHomeConsumption.ui.activity.wallet.ProgressDrawableAlertDialog;
import com.zhsj.OkeHomeConsumption.ui.activity.wallet.TimeOutHandler;
import com.zhsj.OkeHomeConsumption.utils.LogUtils;
import com.zhsj.OkeHomeConsumption.utils.SignUtil;

import java.util.HashMap;

/**
 * * <p>文件描述：商家入驻逻辑层<p>
 */
public class FreshfruitPresenter {
    public FreshfruitUIInterFace mUIInterFace;
    private ProgressDrawableAlertDialog pDialogUtils = null;
    public TimeChecker timeChecker;

    public FreshfruitPresenter(Context mContext, FreshfruitUIInterFace uiInterface) {
        mUIInterFace = uiInterface;
        pDialogUtils = new ProgressDrawableAlertDialog(mContext);
        timeChecker = new TimeChecker(TimeOutHandler.timeOutHandler, Constants.TIME_OUT);
    }


    /**
     * 获取店铺列表
     *
     * @param
     */
    public void ShopListPost(String categoryLevel1,String categoryLevel2, int pageNum,int cityId,double userLongitude,double userLatitude) {
        HashMap<String, String> paramMaps = new HashMap<String, String>();
        final HashMap<String, String> mLogOnHashMap = new HashMap<String, String>();
        mLogOnHashMap.put("v", Constants.APPVERSION);
        mLogOnHashMap.put("t", Constants.TIME);
        mLogOnHashMap.put("deviceToken", Constants.DEVICETOKEN);
        mLogOnHashMap.put("client", Constants.CLIENT);
        mLogOnHashMap.put("deviceId", URLEntity.getInstance().getImei());
        mLogOnHashMap.put("platform", Constants.PLAFORM);


        paramMaps.put("categoryLevel1",categoryLevel1); //一级分类
        mLogOnHashMap.put("categoryLevel1", String.valueOf(categoryLevel1));

        paramMaps.put("categoryLevel2",categoryLevel2); //一级分类
        mLogOnHashMap.put("categoryLevel2", String.valueOf(categoryLevel2));

        paramMaps.put("districCountryId",String.valueOf(cityId));//市
        mLogOnHashMap.put("districCountryId", String.valueOf(cityId));



        //页码
        paramMaps.put("pageNum", String.valueOf(pageNum));
        mLogOnHashMap.put("pageNum", String.valueOf(pageNum));
        //每页的数量
        paramMaps.put("pageSize",String.valueOf(10));
        mLogOnHashMap.put("pageSize", String.valueOf(10));

        paramMaps.put("userLongitude",String.valueOf(userLongitude));
        mLogOnHashMap.put("userLongitude", String.valueOf(userLongitude));

        paramMaps.put("userLatitude",String.valueOf(userLatitude));
        mLogOnHashMap.put("userLatitude", String.valueOf(userLatitude));
        // 网络计时器及线程
        TimeOutHandler.pDialogUtils = pDialogUtils;
        pDialogUtils.show();
        timeChecker.start();
        // 获取sign签名
        Constants.SIGN = SignUtil.getInstance().getSign(mLogOnHashMap);
        HttpClient.get(OkeHomeConsumptionApi.ELVDOU_QUERYSHOPBYCONDITION, paramMaps, new JsonResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                super.onSuccess(statusCode, content);
                timeChecker.check();
                pDialogUtils.dismiss();
                TimeOutHandler.pDialogUtils = null;
                mUIInterFace.ShopListPost(content);
            }

            @Override
            public void onFailure(Throwable e) {
                super.onFailure(e);
                timeChecker.check();
                pDialogUtils.dismiss();
                TimeOutHandler.pDialogUtils = null;
            }
        });
    }
    /**
     * 店铺二级分类
     *
     * @param
     */
    public void MerchantTypeGet(String categoryId, String countyId) {
        HashMap<String, String> paramMaps = new HashMap<String, String>();
        final HashMap<String, String> mLogOnHashMap = new HashMap<String, String>();
        mLogOnHashMap.put("v", Constants.APPVERSION);
        mLogOnHashMap.put("t", Constants.TIME);
        mLogOnHashMap.put("deviceToken", Constants.DEVICETOKEN);
        mLogOnHashMap.put("client", Constants.CLIENT);
        mLogOnHashMap.put("deviceId", URLEntity.getInstance().getImei());
        mLogOnHashMap.put("platform", Constants.PLAFORM);
        /**
         *
         * categoryId
         * 分类标识,如果为空则检索一级分类,否则检索子级
         * */
        mLogOnHashMap.put("categoryId", categoryId);
        paramMaps.put("categoryId",categoryId);

        mLogOnHashMap.put("countyId",countyId);
        paramMaps.put("countyId",countyId);

        LogUtils.e("店铺二级参数", mLogOnHashMap.toString());
        // 网络计时器及线程
        TimeOutHandler.pDialogUtils = pDialogUtils;
        pDialogUtils.show();
        timeChecker.start();
        // 获取sign签名
        Constants.SIGN = SignUtil.getInstance().getSign(mLogOnHashMap);
        HttpClient.get(OkeHomeConsumptionApi.ELVDOU_QUERYBYPARENTIDANDCOUNTYID, paramMaps, new JsonResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                super.onSuccess(statusCode, content);
                LogUtils.e("店铺二级数据", content);
                timeChecker.check();
                pDialogUtils.dismiss();
                TimeOutHandler.pDialogUtils = null;
                mUIInterFace.MerchantTypeGet(content);
            }

            @Override
            public void onFailure(Throwable e) {
                super.onFailure(e);
                timeChecker.check();
                pDialogUtils.dismiss();
                TimeOutHandler.pDialogUtils = null;
            }
        });
    }
    /**
     * 店铺三级分类
     *
     * @param
     */
    public void MerchantTypeThreeGet(String categoryId) {
        HashMap<String, String> paramMaps = new HashMap<String, String>();
        final HashMap<String, String> mLogOnHashMap = new HashMap<String, String>();
        mLogOnHashMap.put("v", Constants.APPVERSION);
        mLogOnHashMap.put("t", Constants.TIME);
        mLogOnHashMap.put("deviceToken", Constants.DEVICETOKEN);
        mLogOnHashMap.put("client", Constants.CLIENT);
        mLogOnHashMap.put("deviceId", URLEntity.getInstance().getImei());
        mLogOnHashMap.put("platform", Constants.PLAFORM);
        /**
         *
         * categoryId
         * 分类标识,如果为空则检索一级分类,否则检索子级
         * */
        mLogOnHashMap.put("categoryId", String.valueOf(categoryId));
        paramMaps.put("categoryId",String.valueOf(categoryId));
        // 网络计时器及线程
        TimeOutHandler.pDialogUtils = pDialogUtils;
        pDialogUtils.show();
        timeChecker.start();
        // 获取sign签名
        Constants.SIGN = SignUtil.getInstance().getSign(mLogOnHashMap);
        HttpClient.get(OkeHomeConsumptionApi.ELVDOU_QUERYBYPARENTID, paramMaps, new JsonResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                super.onSuccess(statusCode, content);
                timeChecker.check();
                pDialogUtils.dismiss();
                TimeOutHandler.pDialogUtils = null;
                mUIInterFace.MerchantTypeThreeGet(content);
            }

            @Override
            public void onFailure(Throwable e) {
                super.onFailure(e);
                timeChecker.check();
                pDialogUtils.dismiss();
                TimeOutHandler.pDialogUtils = null;
            }
        });
    }

    public void IndexBannerGET(int module) {
        HashMap<String, String> paramMaps = new HashMap<String, String>();
        final HashMap<String, String> mLogOnHashMap = new HashMap<String, String>();
        mLogOnHashMap.put("v", Constants.APPVERSION);
        mLogOnHashMap.put("t", Constants.TIME);
        mLogOnHashMap.put("deviceToken", Constants.DEVICETOKEN);
        mLogOnHashMap.put("client", Constants.CLIENT);
        mLogOnHashMap.put("deviceId", URLEntity.getInstance().getImei());
        mLogOnHashMap.put("platform", Constants.PLAFORM);

        mLogOnHashMap.put("mark", "10");
        paramMaps.put("mark","10");

        mLogOnHashMap.put("module", String.valueOf(module));
        paramMaps.put("module",String.valueOf(module));
        // 网络计时器及线程
        TimeOutHandler.pDialogUtils = pDialogUtils;
        pDialogUtils.show();
        timeChecker.start();
        // 获取sign签名
        Constants.SIGN = SignUtil.getInstance().getSign(mLogOnHashMap);
        HttpClient.get(OkeHomeConsumptionApi.MONEY_LUNBOTU, paramMaps, new JsonResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                super.onSuccess(statusCode, content);
                timeChecker.check();
                pDialogUtils.dismiss();
                com.zhsj.OkeHomeConsumption.other.TimeOutHandler.pDialogUtils = null;
                mUIInterFace.IndexBannerGET(content);
            }

            @Override
            public void onFailure(Throwable e) {
                super.onFailure(e);
                timeChecker.check();
                pDialogUtils.dismiss();
                com.zhsj.OkeHomeConsumption.other.TimeOutHandler.pDialogUtils = null;
            }
        });
    }
}
